﻿Namespace DotNetNuke.Modules.UsersOnline.Business
    Public Class OnlineUsersController
        Public Function GetStatistics(ByVal portalID As Int32, ByVal includeHosts As Boolean) As StatisticsInfo
            Dim statistics As New StatisticsInfo
            Dim reader As IDataReader = Nothing

            Try
                reader = Data.DataProvider.Instance().GetStatistics(portalID, includeHosts)

                If (reader.Read()) Then
                    statistics.AnonymousUserCount = reader.GetInt32(0)
                End If
                reader.NextResult()

                If (reader.Read()) Then
                    statistics.OnlineUserCount = reader.GetInt32(0)
                End If
                reader.NextResult()

                If (reader.Read()) Then
                    statistics.LastUser.UserId = reader.GetInt32(0)
                    statistics.LastUser.UserName = reader.GetString(1)
                    statistics.LastUser.DisplayName = reader.GetString(2)
                    statistics.LastUser.FirstName = reader.GetString(3)
                    statistics.LastUser.LastName = reader.GetString(4)
                    statistics.LastUser.FullName = reader.GetString(5)
                    statistics.LastUser.TabID = DotNetNuke.Common.Utilities.Null.NullInteger
                    statistics.LastUser.TabName = DotNetNuke.Common.Utilities.Null.NullString
                    statistics.LastUser.TabIsSecure = DotNetNuke.Common.Utilities.Null.NullBoolean
                End If
                reader.NextResult()

                If (reader.Read()) Then
                    statistics.MembershipCount = reader.GetInt32(0)
                End If
                reader.NextResult()

                If (reader.Read()) Then
                    statistics.MembershipTodayCount = reader.GetInt32(0)
                End If
                reader.NextResult()

                If (reader.Read()) Then
                    statistics.MembershipYesterdayCount = reader.GetInt32(0)
                End If

                reader.Close()
                reader.Dispose()
                reader = Nothing

                ' Remaining fields are calculated.
                statistics.OnlineTotalCount = statistics.AnonymousUserCount + statistics.OnlineUserCount

                Return statistics
            Finally
                If Not IsNothing(reader) Then
                    If Not reader.IsClosed Then reader.Close()
                    reader.Dispose()
                    reader = Nothing
                End If
            End Try
        End Function

        Public Function GetOnlineUsers(ByVal PortalId As Integer, ByVal IncludeHosts As Boolean) As System.Collections.Generic.List(Of OnlineUserInfo)
            Return DotNetNuke.Common.Utilities.CBO.FillCollection(Of OnlineUserInfo)(Data.DataProvider.Instance().GetOnlineUsers(PortalId, IncludeHosts))
        End Function
    End Class
End Namespace

